package com.indexingsystem.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.*;
import com.indexingsystem.boss.admin.mapper.IdxProductThemeRelationMapper;
import com.indexingsystem.boss.admin.mapper.IdxProductsThemeWordsMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.IdxProducts;
import com.indexingsystem.boss.entity.IdxProductsThemeWords;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.boss.utils.ProductIndexingExcelUtil;
import com.indexingsystem.boss.utils.ProductIndexingXmlUtil;
import com.indexingsystem.system.service.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class IdxProductsThemeWordsServiceImpl implements IdxProductsThemeWordsService {

    @Resource
    private IdxProductsThemeWordsMapper idxProductsThemeWordsMapper;

    @Resource
    private IdxProductThemeRelationMapper idxProductThemeRelationMapper;

    @Resource
    private IdxTaskService idxTaskService;

    @Resource
    private IdxTaskProductService idxTaskProductService;

    @Resource
    private IdxProductsService idxProductsService;

    @Resource
    private IdxExportIndexingProductService idxExportIndexingProductService;

    @Override
    public int deleteByPrimaryKey(String id) {
        return idxProductsThemeWordsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(IdxProductsThemeWords record) {
        return idxProductsThemeWordsMapper.insert(record);
    }

    @Override
    public int insertSelective(IdxProductsThemeWords record) {
        return idxProductsThemeWordsMapper.insertSelective(record);
    }

    @Override
    public IdxProductsThemeWords selectByPrimaryKey(String id) {
        return idxProductsThemeWordsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(IdxProductsThemeWords record) {
        return idxProductsThemeWordsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(IdxProductsThemeWords record) {
        return idxProductsThemeWordsMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<IdxProductsThmeDateVo> getProductsThemeWordsList(IdxProductSelectReqVo idxProductSelectReqVo) {
        return idxProductsThemeWordsMapper.getProductsThemeWordsList(idxProductSelectReqVo);
    }

    @Override
    public Integer deleteByIdBatch(List<String> list) {
        return idxProductsThemeWordsMapper.deleteByIdBatch(list);
    }

    @Override
    public PagedResult<IdxProductsThmeDateVo> getProductThemeWordsPageList(IdxGetProductThemeWordsList idxGetProductThemeWordsList) {
        Integer pageNo = idxGetProductThemeWordsList.getPageNo() == null ? 1 : idxGetProductThemeWordsList.getPageNo();
        Integer pageSize = idxGetProductThemeWordsList.getPageSize() == null ? 10 : idxGetProductThemeWordsList.getPageSize();
        //startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtils.toPagedResult(idxProductsThemeWordsMapper.getProductThemeWordsPageList(idxGetProductThemeWordsList));
    }

    @Override
    public Integer getProductThemeWordsPageListCount(IdxGetProductThemeWordsList idxGetProductThemeWordsList) {
        return idxProductsThemeWordsMapper.getProductThemeWordsPageListCount(idxGetProductThemeWordsList);
    }

    /**
     * @Discription 获取标引列表
     * @Author mineary
     * @Date 9:48 2018/11/2
     * @Param [reqVo]
     * @Return java.util.List<com.indexingsystem.boss.Vo.ExportThemeWordsResVo>
     */
    @Override
    public List<ExportThemeWordsResVo> getExportIndexingThemeWordsList(ExportThemeWordsReqVoSingle reqVo) {
        List<ExportThemeWordsResVo> list = new ArrayList<>();
        List<String> taskIds = new ArrayList<>();
        int i = -1;

        //判断ProductId是否为空
        if (reqVo.getProductId() != null && reqVo.getProductId().size() > 0) {
            taskIds.add(String.valueOf(reqVo.getTaskId()));
        } else {
            if (idxTaskService.getArrchildidsByTaskId(reqVo.getTaskId()) != null) {
                taskIds = idxTaskService.getArrchildidsByTaskId(reqVo.getTaskId());
            } else {
                taskIds.add(String.valueOf(reqVo.getTaskId()));
            }
        }

        list = idxProductsThemeWordsMapper.getExportThemeWordsResVoList(reqVo, taskIds);

        List<String> productIds = idxTaskProductService.getProductsByTaskId(taskIds);

        if (reqVo.getProductId() != null && reqVo.getProductId().size() > 0) {
            productIds = reqVo.getProductId();
        }

        if (productIds != null && productIds.size() > 0) {
            for (String id : productIds) {
                ExportThemeWordsResVo vo = new ExportThemeWordsResVo();
                IdxProducts products = idxProductsService.selectByPrimaryKey(id);
                if (products == null) {
                    continue;
                }
                vo.setProductId(products.getProductId());
                vo.setProductName(products.getName());
                if (list != null && list.size() > 0) {
                    for (ExportThemeWordsResVo resVo : list) {
                        if (resVo.getProductId().equals(vo.getProductId())) {
                            i = list.indexOf(resVo);
                        }
                    }
                    if (i < 0) {
                        list.add(vo);
                    } else {
                        i = -1;
                    }
                } else {
                    list.add(vo);
                }
            }
        }


        for (ExportThemeWordsResVo e : list) {
            if (e.getTables() != null) {
                for (ThemeTableResVo t : e.getTables()) {
                    for (ThemeWordsResVo w : t.getThemeWords()) {
                        ProductsRtwReqVo rtwReqVo = new ProductsRtwReqVo();
                        rtwReqVo.setProductId(e.getProductId());
                        rtwReqVo.setThemeTableId(t.getThemeTableId());
                        rtwReqVo.setThemeWordsId(w.getThemeWordsId());
                        List<RelationThemeWordsResVo> rtwList = idxProductThemeRelationMapper.getRelationWordsList(rtwReqVo);
                        w.setRelations(rtwList);
                    }
                }
            }
            e.setExportTime(new Date());
            e.setExportType(reqVo.getExportType());
        }

        for (ExportThemeWordsResVo vo : list) {
            System.out.println("*************************");
            System.out.println(vo.getProductId());
            System.out.println(vo.getProductName());
            System.out.println(vo.getExportTime().toString());
            System.out.println(vo.getExportType());
        }

        return list;

    }

    /**
     * @Discription 导出标引列表
     * @Author mineary
     * @Date 9:49 2018/11/2
     * @Param [reqVo]
     * @Return java.lang.String
     */
    @Override
    public String exportProductIndexing(ExportThemeWordsReqVo reqVo, ExportThemeWordsReqVoSingle reqVoSingle) throws IOException {
        ExportIndexingProductReqVo record = new ExportIndexingProductReqVo();
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        String dateFilePathStr = df2.format(new Date());
        String filepath = reqVo.getExportPath();
        String exportPath = null;
        //设置导出记录参数。
        if (reqVo.getCreateBy() == null) {
            record.setCreateBy("unknown");
        }
        record.setCreateBy(reqVo.getCreateBy());
        if (reqVo.getCreateTime() == null) {
            record.setCreateTime(new Date());
        }
        record.setPathCode(reqVo.getPathCode());
        record.setThemeTableId(reqVo.getThemeTableId());
        record.setExportType(reqVo.getExportType());
        record.setTaskId(reqVoSingle.getTaskId());
        record.setDescription(reqVo.getDescription());
        if (filepath.endsWith("\\")) {
            exportPath = filepath + dateFilePathStr;
        } else {
            exportPath = filepath + "\\" + dateFilePathStr;
        }
        record.setExportPath(exportPath);
        List<ExportThemeWordsResVo> themeWordsList = getExportIndexingThemeWordsList(reqVoSingle);
        if (themeWordsList != null && themeWordsList.size() > 0) {
            if (reqVo.getExportType().toLowerCase().equals("xml")) {
                ProductIndexingXmlUtil xmlUtil = new ProductIndexingXmlUtil();
                for (ExportThemeWordsResVo e : themeWordsList) {
                    //导出标引结果
                    xmlUtil.exportIndexing(e, reqVo.getExportPath());
                    record.setId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
                    record.setProductId(e.getProductId());
                    //插入导出标引记录
                    idxExportIndexingProductService.insertSelectiveByReq(record);
                }
            } else if (reqVo.getExportType().toLowerCase().equals("excel")) {
                ProductIndexingExcelUtil excelUtil = new ProductIndexingExcelUtil();
                for (ExportThemeWordsResVo e : themeWordsList) {
                    //导出标引结果
                    HSSFWorkbook wb = excelUtil.doExportXLS(e, e.getProductName());
                    excelUtil.ExportWbToFile(wb, reqVo.getExportPath(), e.getProductId());
                    record.setId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
                    record.setProductId(e.getProductId());
                    //插入导出标引记录
                    idxExportIndexingProductService.insertSelectiveByReq(record);
                }
            } else {
                return "文件类型无法识别！";
            }
        } else {
            return "产品标引结果为空！";
        }
        return null;
    }


}
